/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 19182
 * Date: 2025-07-17
 * Time: 9:46
 */
public class MySingleList implements ILinkedList{
    static class ListNode{
        int val;
        ListNode next;
        public ListNode(int val){
            this.val = val;
            this.next = null;
        }
    }
    public ListNode head;
    @Override
    public void addFirst(int data) {
        ListNode Node = new ListNode(data);
        Node.next = head;
        head = Node;
    }

    @Override
    public void addLast(int data) {
        ListNode Node = new ListNode(data);
        if(head == null){
            head = Node;
            return;
        }
        ListNode tail = head;
        while(tail.next != null){
            tail = tail.next;
        }
        tail.next = Node;
    }
    private void checkPos(int index) {
        if(index < 0 || index > size()){    //插入的位置不合法
            throw new CheckPosException("index位置不合法" + index);
        }
    }
    private ListNode findIndex(int index) {
        ListNode cur = head;
        int count = 0;
        while(count < index - 1){
            cur = cur.next;
            count++;
        }
        return cur;
    }
    @Override
    public void addIndex(int index, int data) {
        checkPos(index);    //检查位置是否合法
        if(index == 0){
            addFirst(data);
            return;
        }
        if(index == size()){
            addLast(data);
            return;
        }
        ListNode cur = findIndex(index);
        ListNode node = new ListNode(data);
        node.next = cur.next;
        cur.next = node;


    }

    @Override
    public boolean contains(int key) {
       ListNode cur = head;
       while(cur != null){
           if(cur.val == key){
               return true;
           }
           cur = cur.next;
       }
       return false;
    }
    private ListNode search(int key) { //重点
        ListNode cur = head;
        while(cur.next != null){        //不可能尾节点后面还有值，只能搜查到尾节点
            if(cur.next.val == key){
                return cur;
            }
            cur = cur.next;
        }
        return null;
    }
    @Override
    public void remove(int key) {
        if(head == null){
            System.out.println("链表是空的, 无法进行删除");
            return;
        }
        if(head.val == key) {   //头删
            head = head.next;
            return;
        }
        ListNode cur = search(key);
        if(cur == null){
            System.out.println("没有你想删除的节点" + key);
            return;
        }
        cur.next = cur.next.next;

    }



    @Override
    public void removeAllKey(int key) {
        if(head == null){
            System.out.println("链表是空的，不能删除元素");
            return;
        }
        ListNode prev = head;
        ListNode cur = head.next;
        while(cur != null){
            if(cur.val == key){ //我们就是要把prev节点的next指针一定指向到不是key的节点，这里让他直接指向cur.next节点是因为cur遍历过程中总有不是key的节点，就算全是key也会指向null
                prev.next = cur.next;
                cur = cur.next;
            }else{
                prev = cur; //当cur遍历到不是key的节点的时候，prev已经指向他了，这个时候保证prev指向的是非key节点，可以更新prev节点了
                cur = cur.next;
            }
        }
       if(head.val == key){ //处理头结点是key的情况
           head = head.next;
       }
    }

    @Override
    public int size() {
        ListNode cur = head;
        int count =  0;
        while(cur != null){
            cur = cur.next;
            count++;
        }
        return count;
    }

    @Override
    public void clear() {
//        ListNode cur = head;
//        while (cur != null) {
//            //cur.val = null;
//            ListNode curN = cur.next;
//            cur.next = null;
//            cur = curN;
//        } //这里这种写法用于元素是引用类型的，要把他们手动设置空
        head = null;
        ListNode cur = head;
        while (cur != null){
            cur.val = 0;
            cur = cur.next;
        }
        head = null;    //基本类型可以直接把头结点设置null, 头结点没有引用就会被jvm回收，头结点被回收了，他引用的节点也没被头节点引用了，也会回收
    }

    @Override
    public void display() {
        ListNode cur = head;
        while(cur != null){
            System.out.print(cur.val+" ");
            cur = cur.next;
        }
        System.out.println();
    }
}
